﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataObjects.Interface.Production;
using BusinessObjects.Production;
using DataObjects.AdoNet;
using System.Data;

namespace DataObjects.SqlServer.Production
{
    public class SqlServerInwardOutwardDao : IInwardOutwardDao
    {
        public List<InwardOutward> GetInwardOutwards() { return null; }
        public List<InwardOutward> GetInwardOutwardsByRefType(string refType)
        {
            string procedures = "Get_All_InwardOutwards_ByCondition";

            object[] parms = { "@WhereClause", " WHERE RefType = " + refType };
            return Db.ReadList(procedures, true, Make, parms);
        }
        public List<InwardOutward> GetInwardOutwardsByBranch(string refType, string restaurantId)
        {
            string procedures = "Get_All_InwardOutwards_ByCondition";

            object[] parms = { "@WhereClause", " WHERE RestaurantID = '" + restaurantId + "' " +
                                                       " RefType = " + refType };
            return Db.ReadList(procedures, true, Make, parms);
        }
        public InwardOutward GetInwardOutward(string refId)
        {
            string procedures = "Get_All_InwardOutward_ByID";

            object[] parms = { "@RefID", refId };
            return Db.Read(procedures, true, Make, parms);
        }

        public List<InwardOutward> GetInwardOutwardsPagedByBranch(int refType, string restaurantId, int currentPage, bool isMaterial)
        {
            string procedures = "Get_Paged_InwardOutwards";
            int _isMaterial = 0;

            if (isMaterial == true) { _isMaterial = 1; }
            else { _isMaterial = 0; }


            object[] parms = {  "@WhereClause", " RestaurantID = '" + restaurantId + "' " +
                                                       " AND RefType = " + refType +
                                                       " AND IsMaterial = " + _isMaterial, 
                                "@OrderBy", null ,
                                "@PageIndex", currentPage};
            return Db.ReadList(procedures, true, Make, parms);
        }

        public int GetTotalPageByBranch(int refType, string restaurantId, bool isMaterial)
        {
            string procedures = "Get_PageSize_InwardOutward";
            int _isMaterial = 0;

            if (isMaterial == true) { _isMaterial = 1; }
            else { _isMaterial = 0; }

            object[] parms = { "@WhereClause", " WHERE RestaurantID = '" + restaurantId + "' " +
                                                       " AND RefType = " + refType +
                                                       " AND IsMaterial = " + _isMaterial };

            return Db.ReadTotalPage(procedures, true, parms);
        }
        public void InsertInwardOutward(InwardOutward inwardOutward)
        {
            string sql = "Insert_InwardOutward";

            inwardOutward.RefID = Db.Insert(sql, true, Take(inwardOutward));
        }
        public void UpdateInwardOutward(InwardOutward inwardOutward)
        {
            string sql = "Update_InwardOutward";

            Db.Update(sql, true, Take(inwardOutward));
        }
        public void DeleteInwardOutward(InwardOutward inwardOutward)
        {
            string sql = "Delete_InwardOutward";

            Db.Update(sql, true, Take(inwardOutward.RefID));
        }

        private object[] Take(string refId)
        {
            return new object[]  
            {
                "@RefID", refId,
            };
        }

        private object[] Take(InwardOutward inwardOutward)
        {
            return new object[]  
            {
                "@RefID", inwardOutward.RefID,
                "@RefType", inwardOutward.RefType,
                "@RefDate", inwardOutward.RefDate,
                "@PostedDate", inwardOutward.PostedDate,
                "@RefNo", inwardOutward.RefNo,
                "@EmployeeID", inwardOutward.EmployeeID,
                "@AccountingObjectID", inwardOutward.AccountingObjectID,
                "@AccountingObjectName", inwardOutward.AccountingObjectName,
                "@AccountingObjectAddress", inwardOutward.AccountingObjectAddress,
                "@JournalMemo", inwardOutward.JournalMemo,
                "@CurrencyID", inwardOutward.CurrencyID,
                "@ExchangeRate", inwardOutward.ExchangeRate,
                "@IsImportPurchase", inwardOutward.IsImportPurchase,
                "@ShippingMethodID", inwardOutward.ShippingMethodID,
                "@TotalAmount", inwardOutward.TotalAmount,
                "@RestaurantID", inwardOutward.RestaurantID,
                "@IsPosted", inwardOutward.IsPosted,
                "@IsMaterial", inwardOutward.IsMaterial
            };
        }

        private static Func<IDataReader, InwardOutward> Make = reader =>
            new InwardOutward
            {
                RefID = reader["RefID"].AsString(),
                RefType = reader["RefType"].AsInt(),
                RefDate = reader["RefDate"].AsDateTime(),
                PostedDate = reader["PostedDate"].AsDateTime(),
                RefNo = reader["RefNo"].AsString(),
                EmployeeID = reader["EmployeeID"].AsString(),
                AccountingObjectID = reader["AccountingObjectID"].AsString(),
                AccountingObjectName = reader["AccountingObjectName"].AsString(),
                AccountingObjectAddress = reader["AccountingObjectAddress"].AsString(),
                JournalMemo = reader["JournalMemo"].AsString(),
                CurrencyID = reader["CurrencyID"].AsString(),
                ExchangeRate = reader["ExchangeRate"].AsDecimal(),
                IsImportPurchase = reader["IsImportPurchase"].AsBool(),
                ShippingMethodID = reader["ShippingMethodID"].AsString(),
                TotalAmount = reader["TotalAmount"].AsDecimal(),
                RestaurantID = reader["RestaurantID"].AsString(),
                IsPosted = reader["IsPosted"].AsBool(),
                IsMaterial = reader["IsMaterial"].AsBool()
            };
    }
}
